#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include "messageProcessor.h"
void *writer(void *arg){
	int t=*(int*)arg;
	int i;
	for(i=0;i<10;i++){
		//sleep(t);
		int iMeg=i*10000+t*100;
		int size=writeMessage((unsigned char*)&iMeg, sizeof(iMeg));
		fprintf(stderr,"%d thread send Message : %d size: %d\n",t,iMeg,size);
	}
}
void *reader(void *arg){
	int cnt=*(int*)arg;
	int i;
	unsigned char buf[MAX_QUEUE_BUF_SIZE];
	for(i=0;i<cnt;i++){
		int size=readMessage(buf);
		int iMeg=*(int*)buf;
		fprintf(stderr,"read Message : %d size :%d\n",iMeg,size);
		sleep(3);
	}
}
int main(void){
	pthread_attr_t p_attr[5];
	pthread_attr_t c_attr;
	pthread_t p_tid[5];
	pthread_t c_tid;
	int i;
	int cnt=5*10;
	int args[]={1,2,3,4,5};
	initQueue();
	for(i=0;i<5;i++)
		pthread_attr_init(p_attr+i);
	pthread_attr_init(&c_attr);
	for(i=0;i<5;i++)
		pthread_create(p_tid+i,p_attr+i,writer,(void*)(args+i));
	pthread_create(&c_tid,&c_attr,reader,(void*)&cnt);
	for(i=0;i<5;i++)
		pthread_join(p_tid[i],NULL);
	pthread_join(c_tid,NULL);
	return 0;

}
